<?php

namespace app\index\controller;
use think\Controller;
use think\facade\Hook;
use think\Db;
use app\common\service\EsTool;
use think\Console;
use app\common\helper\ElasticSearch;
use app\common\helper\ProductSearchBuilder;

class Etest extends Controller
{

    public function test()
    {
        return "yong you yi zheng ge yuzhou";
    }

    public function index()
    {

        $es = new ElasticSearch();
        $re = $es->queryData('products_test', '12', '');
        return $re;

    }

    // es构造器测试
    public function esBuilder()
    {
        $es = new ElasticSearch();
        $esBuilder = new ProductSearchBuilder();

        // 构造es搜索参数
        $esBuilder->paginate(20, 1)->keywords("12");
        $param = $esBuilder->getParams();
        $buildRes = $es->search($param);

return $buildRes;

        $param = [
            'index' => "products_test",
            'client' => [
                'timeout' => 10,        // ten second timeout
                'connect_timeout' => 10
            ],
            'body' => [
                'from' => 0,
                'size' => 20,
                'query' => [
                    'bool' => [
                        'should' => [
                            [
                                'multi_match' => [
                                    'query' => '12',
                                    'fields' => [
                                        'goods_name^2',
                                        'single_goods_name'
                                    ],
                                ],
                            ],
                            [
                                'wildcard' => [
                                    'single_goods_name' => "12*"
                                ]
                            ],
                            [
                                'fuzzy' => [
                                    'single_goods_name' => [
                                        'value' => 12
                                    ]
                                ]
                            ]
                        ],
                    ],
                ],
                'sort' => [
                    ['_score' => "desc"],
                    ['goods_sort' => 'desc']
                ],
            ]
        ];

        $re = $es->search($param);
return $re;

        // $esBuilder = new ProductSearchBuilder();
        // $re = $esBuilder;

    }

}
